package com.itheima.ArgumentResolver;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @ClassName MemberArgumentResolver
 * @Description: TODO
 * @Author hp
 * @Date 2020/2/12
 * @Version V1.0
 **/
public class MemberArgumentResolver implements HandlerMethodArgumentResolver{

    @Autowired
    private JedisPool jedisPool;

    //判断是否需要参数解析,或者说是参数解析需要满足的条件

    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //判断参数上是否有此注解
        return methodParameter.hasParameterAnnotation(Member.class);
    }

    //真正参数解析的方法

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        Jedis jedis = jedisPool.getResource();
        String token = nativeWebRequest.getHeader("token");
        String s = jedis.get(token);
        return JSON.parseObject(s, com.itheima.pojo.Member.class);
    }
}
